package offer;

import java.util.HashSet;

public class Ex48 {
    public int lengthOfLongestSubstring(String s) {
        int left = 0;
        int right = 0;
        int max = 0;
        HashSet<Character> ch = new HashSet<>();
        while(right<s.length()){
            if(ch.add(s.charAt(right))){
                right++;
            }else{
                while (left<right){
                    ch.remove(s.charAt(left));
                    left++;
                    if(!ch.contains(s.charAt(right))){
                        break;
                    }
                }
            }
            max = Math.max(max,right-left);
        }
        return max;
    }

    public static void main(String[] args) {
        Ex48 ex48 = new Ex48();
        ex48.lengthOfLongestSubstring("abcabcbb");
    }
}
